package service.cms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import service.cms.entity.CrmBanner;
import service.cms.service.CrmBannerService;
import service.edu.result.R;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-08-13
 */
@RestController
@CrossOrigin
@RequestMapping("/cms/adminBanner")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**分页查询轮播图
     *current:当前页
     *limit:每页记录数
     */
    @ApiOperation("分页查询banner")
    @GetMapping("/query/{current}/{limit}")
    public R queryBannerByPage(@ApiParam(name = "current",value = "当前页数",required =true)
                               @PathVariable("current")long current,
                               @ApiParam(name = "limit",value = "页数",required =true)
                               @PathVariable("limit")long limit){
        Page<CrmBanner> page = new Page<>(current, limit);
        IPage<CrmBanner> crmBannerIPage = bannerService.page(page, null);
        return R.Ok().data("total", crmBannerIPage.getTotal()).data("rows",crmBannerIPage.getRecords());
    }

    //根据id查询轮播图
    @ApiOperation("根据id查询轮播图")
    @GetMapping("/query/{id}")
    public R queryBannerById(@PathVariable("id")String id){
        CrmBanner banner = bannerService.getById(id);
        return R.Ok().data("banner", banner);
    }

    //增加轮播图
    @ApiOperation("添加轮播图")
    @PostMapping("addBanner")
    public R addBanner(@ApiParam(name = "crmBanner",value = "新增的轮播图")
                       @RequestBody(required = false)CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.Ok();
    }

    //更改轮播图
    @ApiOperation("更新轮播图,id必填")
    @PostMapping("/update")
    public R updateBanner(@ApiParam(name = "crmBanner",value = "修改的轮播图")
                              @RequestBody(required = false)CrmBanner crmBanner){
        bannerService.updateById( crmBanner);
        return R.Ok();
    }

    //删除轮播图
    @ApiOperation("根据id删除轮播图")
    @DeleteMapping("delete/{id}")
    public R deleteBannerById(@PathVariable("id")String id){
        bannerService.removeById(id);
        return R.Ok();
    }


}

